package muhnes.sizoo.dal;

import java.util.List;
import javax.annotation.PreDestroy;
import muhnes.sizoo.HibernateUtilZoologia;
import muhnes.sizoo.modelo.Areaprotegida;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class AreaprotegidaDaoImpl implements AreaprotegidaDao {
    
    Session sesion;
    
    public AreaprotegidaDaoImpl() {
        sesion = HibernateUtilZoologia.getSessionFactory().openSession();
    }
    
    @PreDestroy
    public void destroy() {
        sesion.close();
    }

    @Override
    public void guardar( Areaprotegida areaprotegida ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.save( areaprotegida );
        transaccion.commit();
    }

    @Override
    public void guardarColeccion( List< Areaprotegida > areasprotegidas ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.saveOrUpdate( areasprotegidas );
        transaccion.commit();
    }

    @Override
    public void actualizar( Areaprotegida areaprotegida ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( areaprotegida );
        transaccion.commit();
    }

    @Override
    public void actualizarColeccion( List< Areaprotegida > areasprotegidas ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.update( areasprotegidas );
        transaccion.commit();
    }

    @Override
    public void eliminar( Areaprotegida areaprotegida ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( areaprotegida );
        transaccion.commit();
    }

    @Override
    public void eliminarColeccion( List< Areaprotegida > areasprotegidas ) {
        Transaction transaccion = sesion.beginTransaction();
        sesion.delete( areasprotegidas );
        transaccion.commit();
    }

    @Override
    public void eliminarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Areaprotegida areaprotegida = buscarPorId( id );
        sesion.delete( areaprotegida );
        transaccion.commit();
    }

    @Override
    public Areaprotegida buscarPorId( int id ) {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Areaprotegida where codigoareaprotegida = " + id);
        Areaprotegida areaprotegida = (Areaprotegida)consulta.uniqueResult();
        transaccion.commit();
        return areaprotegida;
    }

    @Override
    public List< Areaprotegida > buscarTodos() {
        Transaction transaccion = sesion.beginTransaction();
        Query consulta = sesion.createQuery("from Areaprotegida");
        List< Areaprotegida > areasprotegidas = consulta.list();
        transaccion.commit();
        return areasprotegidas;
    }

}
